package com.mc.read.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DateUtil {

    /**
     * 日期时间格式
     */
    public static final String timePattern = "HHmmss";
    public static final String timesPattern = "yyyy/MM/ddHH:mm:ss";
    public static final String datePattern = "yyyyMMdd";
    public static final String shortDatePattern = "yyMMdd";
    public static final String fullPattern = "yyyyMMddHHmmss";
    public static final String fullPatterns = "yyyyMMddHHmmssSSS";
    public static final String partPattern = "yyMMddHHmmss";
    public static final String ticketPattern = "yyyy.MM.dd HH:mm:ss";
    public static final String settlePattern = "yyyy-MM-dd HH:mm:ss";
    public static final String todayPattern = "yyyyMMdd";
    public static final String hour_of_minute = "HHmm";
    public static final String timeColPattern = "HH:mm:ss";
    public static final String dateFullPattern = "yyyyMMdd HH:mm:ss";
    public static final String year_of_minute = "yyyyMMddHHmm";
    public static final String yearDate = "yyyy-MM-dd HH:mm";
    public static final String shotPattern = "yyyy-MM-dd";

    public static final String shotSPattern ="yyyy-MM";


    private static final Object lockObj = new Object();

    private static Map<String, ThreadLocal<SimpleDateFormat>> sdfMap = new HashMap<String, ThreadLocal<SimpleDateFormat>>();


    public static String convert(String date, String originPattern,
                                 String targetPattern) throws ParseException {
        Date originDate = parse(date, originPattern);
        return format(originDate, targetPattern);
    }


    public static boolean compareDateForSecond(Date src, Date target, int second) {
        Calendar targetTime = Calendar.getInstance();
        targetTime.setTime(target);
        targetTime.add(Calendar.SECOND, second);
        Calendar srcTime = Calendar.getInstance();
        srcTime.setTime(src);
        return srcTime.compareTo(targetTime) <= 0;
    }


    public static String getCurrentAfter(int minute) {
        Calendar targetTime = Calendar.getInstance();
        targetTime.setTime(new Date());
        targetTime.add(Calendar.MINUTE, minute);
        return format(targetTime.getTime(), DateUtil.fullPattern);
    }


    public static String getCurrent() {
        return format(new Date(), DateUtil.settlePattern);
    }

    public static String getCurrentBySss() {
        return format(new Date(), DateUtil.fullPatterns);
    }


    public static String getTodayCurrent() {
        return format(new Date(), DateUtil.todayPattern);
    }


    private static SimpleDateFormat getSdf(final String pattern) {

        ThreadLocal<SimpleDateFormat> tl = sdfMap.get(pattern);
        if (tl == null) {
            synchronized (lockObj) {
                tl = sdfMap.get(pattern);
                if (tl == null) {
                    tl = new ThreadLocal<SimpleDateFormat>() {
                        @Override
                        protected SimpleDateFormat initialValue() {
                            return new SimpleDateFormat(pattern);
                        }
                    };
                    sdfMap.put(pattern, tl);
                }
            }
        }

        return tl.get();
    }


    public static String format(Date date, String pattern) {
        if (date == null) {
            return "";
        }
        return getSdf(pattern).format(date);
    }


    public static Date parse(String dateStr, String pattern) throws ParseException {
        return getSdf(pattern).parse(dateStr);
    }

    public static Date getNowTime() throws Exception{

        return parse( getCurrent(),settlePattern);
    }


    /**
     * 获取第一天
     *
     * @param dateStr
     * @return
     * @throws Exception
     */
    public static Date getMothFirtDay(String dateStr) throws Exception{
        dateStr=dateStr+"-01 00:00:00";
        Date date =  DateUtil.parse(dateStr, settlePattern);
        return date;
    }

    /**
     * 获取每月最后一天
     *
     * @param dateStr
     * @return
     * @throws Exception
     */
    public static Date getMothLastDay(String dateStr) throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.parse(dateStr, shotSPattern));
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date lastDayOfMonth = calendar.getTime();
        String  lastDayOfMonthStr= DateUtil.format(lastDayOfMonth,shotPattern);
        lastDayOfMonthStr=lastDayOfMonthStr+" 23:59:59";
        Date date =  DateUtil.parse(lastDayOfMonthStr, settlePattern);
//        Date date =  DateUtil.parse(dateStr, settlePattern);
        return date;
    }


}
